package com.example.chapter05;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.chapter05.util.ViewUtil;

import java.util.Random;

public class LoginForgetActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_new_password;
    private EditText et_confirm_new_password_again;
    private Button btn_get_verifyCode;
    private EditText et_verifyCode;
    private Button btn_confirm;
    private String mVerifyCode;
    private String phone;
    private String oldPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_forget);

        et_new_password = findViewById(R.id.et_new_password);
        et_confirm_new_password_again = findViewById(R.id.et_confirm_new_password_again);
        btn_get_verifyCode = findViewById(R.id.btn_get_verifyCode);
        et_verifyCode = findViewById(R.id.et_verifyCode);
        btn_confirm = findViewById(R.id.btn_confirm);

        btn_confirm.setOnClickListener(this);
        btn_get_verifyCode.setOnClickListener(this);

        et_new_password.addTextChangedListener(new HideTextWatcher(et_new_password, 6));
        et_verifyCode.addTextChangedListener(new HideTextWatcher(et_verifyCode, 6));
        et_confirm_new_password_again.addTextChangedListener(new HideTextWatcher
                (et_confirm_new_password_again, 6));

        Bundle bundle = getIntent().getExtras();
        phone = bundle.getString("phone");
        oldPassword = bundle.getString("password");


    }

    @Override
    public void onClick(View v) {
        //点击获取验证码按钮
        if (v.getId() == R.id.btn_get_verifyCode) {
            //先判断确认新密码和输入新密码的内容是否相同
            if (!et_new_password.getText().toString()
                    .equals(et_confirm_new_password_again.getText().toString())){
                //在界面下方显示
                Toast.makeText(this,"两次输入密码不一致，请重新输入",Toast.LENGTH_SHORT)
                        .show();
                et_confirm_new_password_again.setText(null);
                return;
            }
            //新密码和旧密码一致，设置新密码错误
            if (et_new_password.getText().toString().equals(oldPassword)){
                Toast.makeText(this,"新密码和旧密码一致，请重新输入新密码",Toast.LENGTH_SHORT)
                        .show();
                et_new_password.setText(null);
                et_confirm_new_password_again.setText(null);
                return;
            }
            //获取六位验证码
            mVerifyCode = String.format("%06d",new Random().nextInt(999999));
            //弹窗提示
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("请记住新密码");
            builder.setMessage("手机号为："+phone+"当前设置的新密码为："+et_new_password.getText().toString()+","+
                    "验证码为："+ mVerifyCode +",请记住");
            builder.create().show();

        }

        //点击确定按钮
        if(v.getId() == R.id.btn_confirm){
            //判断验证码是否输入正确
            if (!et_verifyCode.getText().toString().equals(mVerifyCode)){
                //在界面下方显示
                Toast.makeText(this,"验证码输入错误，请重新输入",Toast.LENGTH_SHORT)
                        .show();
                et_verifyCode.setText(null);
                return;
            }
            //正确，将新密码返回并返回到主界面
            //将user_info中的密码进行修改
            changePasswordSuccess();

        }
    }

    private void changePasswordSuccess() {
        Intent intent = new Intent(this,LoginMainActivity.class);
        intent.putExtra("newPassword",et_new_password.getText().toString());
        setResult(Activity.RESULT_OK,intent);
        finish();
    }

    private class HideTextWatcher implements TextWatcher {
        private final EditText mView;
        private final int maxLength;

        public HideTextWatcher(EditText et, int maxLength) {
            this.mView = et;
            this.maxLength = maxLength;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            //隐藏软键盘
            if (s.toString().length() == maxLength) {
                ViewUtil.hideOneInputMethod(LoginForgetActivity.this, mView);
            }
        }
    }
}